﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Eshouse.Entity;
using System.Data;

namespace Eshouse.Bll
{
    public class MenuAccess
    {
        public static string GetMenuOnJson(int id)
        {
            DataRow row;
            StringBuilder resultStr = new StringBuilder();
            resultStr.Append("");
            resultStr.Append("[");
            #region  xml方式 
            DataTable dtmenu = new DataTable();
            DataSet ds = new DataSet();
            ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("~/xmlConfig/menuConfig.xml"));
            dtmenu=ds.Tables[0];
            DataRow[] rows;
            if(id==0)
            {
                rows = dtmenu.Select("nodetype='0'");
            }else{
                rows = dtmenu.Select("fatherid='"+id.ToString()+"'");
            }
            for (int i = 0; i < rows.Length; i++)
            {
                row = rows[i];
                string menuID = row["menuid"].ToString();
                if (dtmenu.Select("fatherid='" + menuID + "'").Length==0 && id != 0)
                {
                    resultStr.Append("{");
                    resultStr.Append(string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"state\": \"closed\",\"iconCls\": \"tree_file\",\"addr\": \"{2}\"", menuID, row["menuname"].ToString(), row["menuurl"].ToString()));
                    resultStr.Append("}");
                    if (i < rows.Length - 1)
                        resultStr.Append(",");
                
                }
                else {

                    resultStr.Append("{");
                    resultStr.Append(string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"state\": \"closed\",\"addr\": \"\"", menuID, row["menuname"].ToString()));
                    resultStr.Append("}");
                    if (i < rows.Length - 1)
                        resultStr.Append(",");                                
                }
            }
            #endregion
            #region  数据库方式
            //DatabaseHelper db = new DatabaseHelper();
            //string strSql = "select * from menu where modelID=" + id;
            //if (id != 0)
            //    strSql = "select * from menu where fatherID=" + id;           
            //DataTable dt = db.ExecuteDataTable(strSql);
            //object o = null;           
            ////if (id >= 0)
            ////{
               
            //    resultStr.Append("");
            //    resultStr.Append("[");
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        row = dt.Rows[i];
            //        string menuID = row["menuid"].ToString();
            //        strSql = "select id from menu where fatherID=" + menuID;
            //        o = db.ExecuteScalar(strSql);
            //        if (o == null && id!=0)
            //        {
            //            resultStr.Append("{");
            //            resultStr.Append(string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"state\": \"closed\",\"iconCls\": \"tree_file\",\"addr\": \"{2}\"", menuID, row["menuname"].ToString(), row["menuurl"].ToString()));
            //            resultStr.Append("}");
            //            if (i < dt.Rows.Count - 1)
            //                resultStr.Append(",");
            //        }
            //        else
            //        {   ///icon-doc
            //            resultStr.Append("{");
            //            resultStr.Append(string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"state\": \"closed\",\"addr\": \"\"", menuID, row["menuname"].ToString()));
            //            resultStr.Append("}");
            //            if (i < dt.Rows.Count - 1)
            //                resultStr.Append(",");

            //        }
            //    }               
            //}
            #endregion
            resultStr.Append("]");
            return resultStr.ToString();
        }
    }
}
